<template>
  <div id="app">
    <loading v-model="isLoading"></loading>
    <transition name="fade" mode="out-in">
      <router-view/>
    </transition>
  </div>
</template>

<script>
  import { Loading,querystring } from 'vux'
  import {weiXinAuth,weiXinRedirect} from "./api";

  export default {
    name: 'App',
    components:{
      Loading
    },
    computed: {
      isLoading(){
        return this.$store.getters.getLoadingStatus
      }
    },
    data(){
      return{
      }
    },
    methods:{
    },
    mounted(){
      let params = querystring.parse(window.location.href.split("?")[1])
      let index = params.index?params.index:querystring.parse(window.location.href.split("?")[2]).index
      sessionStorage.setItem('index', index != null?index:0);//存储数据库标识
      console.log('APP page:',window.location.href,params,index,sessionStorage.getItem('index'))
      if(!(sessionStorage.getItem("access_token") && sessionStorage.getItem("refresh_token") && sessionStorage.getItem("openid") &&  JSON.parse(sessionStorage.getItem("user"))))
        weiXinAuth(params.code).then((res)=>{
          if(res.status == 'success') {
            console.log("weiXinAuth返回的值：",res.data)
            sessionStorage.setItem('access_token', res.data["access_token"]);
            sessionStorage.setItem('refresh_token', res.data["refresh_token"]);
            sessionStorage.setItem('openid', res.data["openid"]);
            //sessionStorage.setItem('userid', res.data["userinfo"]["userid"]);
            sessionStorage.setItem("user", JSON.stringify(res.data["userinfo"]));
          }else if(res.status == 'error'){
            console.log('weiXinAuth error info:',res.msg)
            this.$store.commit('updateLoadingStatus',{isLoading:false})
            this.$vux.alert.show({
              title:'获取微信授权失败',
              content:'信息：' + res.msg,
              buttonText:'尝试重新获取',
              onHide () {
                window.location.href = weiXinRedirect//这里要跳到重定向地址
              }
            })
          }
          this.$store.commit('updateLoadingStatus',{isLoading:false})
        }).catch((err)=>{console.log('weiXinAuth catch info:',err)})
    },
    created(){
      if(!localStorage.getItem('debugMode'))localStorage.setItem('debugMode',false)
    },
    beforeDestroy(){

    }
  }
</script>

<style lang="less">
  @import "~vux/src/styles/1px.less";
  .fade-enter-active,.fade-leave-active{transition: all 0.2s;}
  .fade-enter,.fade-leave-active{opacity: 0;}
  *{margin:0;padding:0;}
  html{height:100%;color:#2c3e50;font-family: "Helvetica Neue",Helvetica,"PingFang SC","Hiragino Sans GB","Microsoft YaHei","微软雅黑",Arial,sans-serif;-webkit-font-smoothing: antialiased;-moz-osx-font-smoothing: grayscale;}
  body{height:100%;background-color: rgb(251, 249, 254);}
  img,a:hover img{border:none;max-width: 100%;}
  a{text-decoration:none;cursor:pointer;color:#2c3e50;}
  ul,li{list-style:none;}
  #app{height:100%;}
  .header{position: fixed !important;z-index:200;top: 0;width: 100%;}
  .topHolder{height:46px;}
  .card-padding{padding:1rem;}
  .weui-cell{
    &.number{
      .weui-cell__ft{font-size: 0.9rem;overflow: hidden;text-overflow:ellipsis;white-space: nowrap;}
      &.notDotted{
        .weui-cell__ft{width: auto;overflow: auto;text-overflow: initial;white-space: initial;}
      }
    }
  }
  .listNoItem{width:100%;text-align: center; padding: 1rem 0;color: #a3a3a3;}
  button{
    &.inlineBtn.weui-btn{
      width: auto;display: inline-block;font-size: 0.9rem;margin: 0 0 0 0;
    }
    &.popPickerBtn.weui-btn{
      i{margin-left:0.3rem;font-size: 0.6rem;}
    }
  }
  .btn_popup-picker:before{border-top: none !important;}
  .formPreviewList{
    margin-bottom: 0.5rem;
    .weui-form-preview__hd{
      .weui-form-preview__value{font-size: 1.3rem;}
    }
    &.label{
      .weui-form-preview__hd{
        line-height: 0.5em;
        .weui-form-preview__label{line-height: 1.5em;}
        .weui-form-preview__value {font-size: 0.9rem;display: inline-block;background: #eee;border-radius: 0.3rem;padding: 0 0.6rem;line-height: 1.5rem;}
      }
      .weui-form-preview__bd{
        padding: 0.4rem 1rem;line-height: 1.5rem;
      }
      &.orange{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #ffca10;color: #ffca10;}}}
      &.green{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #0abd39;color: #0abd39;}}}
      &.red{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #ff2623;color: #ff2623;}}}
      &.blue{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #25b3ff;color: #25b3ff;}}}
      &.gray{.weui-form-preview__hd{.weui-form-preview__value {background: #fff;border:1px solid #bebdbb;color: #bebdbb;}}}
    }
  }
  .vux-table td, .vux-table th{line-height: 1.5rem;padding:0.4rem 0.3rem;}
  .vux-table td{font-size: 0.9rem;}
</style>
